package data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;

public class FlexiblePackage extends AbstractData{
	private long id = -1;
	private  long hotelId;
	private String packageName;
	private RoomType roomtype;
	private String roomtypeString;
	private String description;
	private List<GuestType> avaliableFor;
	private String avaliableForString;
	private Date datefrom;
	private Date dateto;
	private List<SubFlexiblePackage> subflexiblepackage;
	private String subflexiblepackagestring;
	private boolean packageControl;
	private boolean status;
	private boolean avaliable;
	private boolean isArchive;
	
	public FlexiblePackage(){}
	
	public FlexiblePackage(long id){
		this.id = id;
		this.status = true;
		this.avaliable = true;
	}
		
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getHotelId() {
		return hotelId;
	}

	public void setHotelId(long hotelId) {
		this.hotelId = hotelId;
	}

	public String getPackageName() {
		return packageName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public RoomType getRoomtype() {
		if (roomtype == null && !(roomtypeString == null || roomtypeString.isEmpty())) {
			RoomType rt = new RoomType(Long.parseLong(roomtypeString));
			rt.LoadData();
			roomtype = rt;
		}
		return roomtype;
	}

	public void setRoomtype(RoomType roomtype) {
		this.roomtypeString = String.format("%d", roomtype.getId());
		this.roomtype = roomtype;
	}

	public String getRoomtypeString() {
		return roomtypeString;
	}

	public void setRoomtypeString(String roomtypeString) {
		this.roomtypeString = roomtypeString;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<GuestType> getAvaliableFor() {
		if ((avaliableFor == null || avaliableFor.size() == 0) && !(avaliableForString == null || avaliableForString.isEmpty())) {
			String[] idOfCounter = SplitList(avaliableForString);
			List<GuestType> list = new ArrayList<GuestType>();
			for (String idCounter : idOfCounter) {
				GuestType c = new GuestType(Long.parseLong(idCounter));
				list.add(c);
			}
			
			for(GuestType c : list)
				c.LoadData();
			
			avaliableFor = list;
		}
		return avaliableFor;
	}

	public void setAvaliableFor(List<GuestType> avaliableFor) {
		this.avaliableForString = CombineList(avaliableFor);
		this.avaliableFor = avaliableFor;
	}

	public String getAvaliableForString() {
		return avaliableForString;
	}

	public void setAvaliableForString(String availableForString) {
		this.avaliableForString = availableForString;
	}

	public Date getDatefrom() {
		return datefrom;
	}

	public void setDatefrom(Date datefrom) {
		this.datefrom = datefrom;
	}

	public Date getDateto() {
		return dateto;
	}

	public void setDateto(Date dateto) {
		this.dateto = dateto;
	}

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public boolean isAvaliable() {
		return avaliable;
	}

	public void setAvaliable(boolean avaliable) {
		this.avaliable = avaliable;
	}

	public List<SubFlexiblePackage> getSubflexiblepackage() {
		if ((subflexiblepackage == null || subflexiblepackage.size() == 0) &&
				!(subflexiblepackagestring == null || subflexiblepackagestring.isEmpty())) {
			String[] idOfCounter = SplitList(subflexiblepackagestring);
			List<SubFlexiblePackage> list = new ArrayList<SubFlexiblePackage>();
			for (String idCounter : idOfCounter) {
				SubFlexiblePackage c = new SubFlexiblePackage(Long.parseLong(idCounter));
				list.add(c);
			}
			
			for(SubFlexiblePackage c : list)
				c.LoadData();
			
			subflexiblepackage = list;
		}
		return subflexiblepackage;
	}

	public void setSubflexiblepackage(List<SubFlexiblePackage> subflexiblepackage) {
		this.subflexiblepackagestring = CombineList(subflexiblepackage);
		this.subflexiblepackage = subflexiblepackage;
	}

	public String getSubflexiblepackagestring() {
		return subflexiblepackagestring;
	}

	public void setSubflexiblepackagestring(String subflexiblepackagestring) {
		this.subflexiblepackagestring = subflexiblepackagestring;
	}

	public boolean isArchive() {
		return isArchive;
	}

	public void setArchive(boolean isArchive) {
		this.isArchive = isArchive;
	}

	public boolean isPackageControl() {
		return packageControl;
	}

	public void setPackageControl(boolean packageControl) {
		this.packageControl = packageControl;
	}

	@Override
	public Entity getEntity() {
		Entity item;
		if(this.key == null)
			item = new Entity(EntityName.FlexiblePackage);
		else
			item = new Entity(this.key);
		item.setProperty("hotelId", this.hotelId);
		item.setProperty("packageName", this.packageName);
		item.setProperty("roomtype", this.roomtypeString);
		item.setProperty("status", this.status);
		item.setProperty("description", this.description);
		item.setProperty("datefrom", this.datefrom);
		item.setProperty("dateto", this.dateto);
		item.setProperty("avaliable", this.avaliable);
		item.setProperty("avaliableFor", this.avaliableForString);
		item.setProperty("subflexiblepackage", subflexiblepackagestring);
		item.setProperty("isArchive", this.isArchive);
		item.setProperty("packageControl", this.packageControl);
		return item;
	}
	@Override
	public void setEntity(Entity entity) {
		this.id = entity.getKey().getId();
		this.hotelId = (Long) entity.getProperty("hotelId");
		this.roomtypeString = (String) entity.getProperty("roomtype");
		this.packageName = (String) entity.getProperty("packageName");
		this.status = (Boolean) entity.getProperty("status");
		this.avaliable = (Boolean) entity.getProperty("avaliable");
		this.avaliableForString = (String) entity.getProperty("avaliableFor");
		this.description = (String) entity.getProperty("description");
		this.datefrom = (Date) entity.getProperty("datefrom");
		this.dateto = (Date) entity.getProperty("dateto");
		this.subflexiblepackagestring = (String) entity.getProperty("subflexiblepackage");
		this.isArchive = (Boolean) entity.getProperty("isArchive");
		this.packageControl = (Boolean) entity.getProperty("packageControl");
		this.key = entity.getKey();
	}
	
	public void LoadData() {
		if (this.id > -1) {
			Entity entity = LoadData(EntityName.FlexiblePackage, this.id);
			setEntity(entity);
		}
	}
	
}
